﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Pixysoft.IO
{
    public class DataTableExporter
    {
        public static string Export(DataTable table)
        {
            table.AcceptChanges();

            StringBuilder builder = new StringBuilder();

            string tablename = table.TableName;
            builder.Append(tablename);
            builder.Append(":");
            builder.Append("\r\n");

            foreach (DataColumn column in table.Columns)
            {
                builder.Append(column.ColumnName);
                builder.Append("\t");
            }

            builder.Append("\r\n");

            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    builder.Append(row[column].ToString());
                    builder.Append("\t");
                }
                builder.Append("\r\n");
            }

            return builder.ToString();
        }

        /// <summary>
        /// 矩阵转置
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static string TransposeExport(DataTable table)
        {
            table.AcceptChanges();

            StringBuilder builder = new StringBuilder();

            string tablename = table.TableName;
            builder.Append(tablename);
            builder.Append(":");
            builder.Append("\r\n");

            foreach (DataColumn column in table.Columns)
            {
                builder.Append(column.ColumnName);
                builder.Append("\t");
                foreach (DataRow row in table.Rows)
                {
                    builder.Append(row[column].ToString());
                    builder.Append("\t");
                }
                builder.Append("\r\n");
            }

            return builder.ToString();
        }
    }
}
